import Block from "../../qbasics/basics/Block";
import  Word from "./Word"
import Row from "../../qbasics/basics/Row";
import Lex from "./Lex";
import Wordr from "../../qbasics/pack/Wordr";
import StrTo from "../../qbasics/utils/StrTo";
export default class LexCore{
    constructor(){
        this.wordr=new Wordr();
        this.word=new Word(this);
        this.lex=new Lex(this);
    }



    run(str){
       return this.word.run(str)
    }
    lexRun(ids){
      let   lexs=new Lex(this);
       return lexs.run(ids)
    }

    /**
     * 获取语法 type
     * @param lexTypes
     */
    yaccType(lexTypes){
        let res=new Array();
        for(let i=0;i<lexTypes.length;i++){
            let lexType=lexTypes[i]
            let id=lexType.wordIdKey?lexType.wordIdKey:lexType.wordsId;
            res.push({data:this.findWord(id),type:this.findYaccType(id)})
        }


        return res;

    }

    /**
     * 用ID 获取 文字
     * @param id
     * @returns {string}
     */
    findWord(id){
       let row=  this.wordr.findRow(id)
        let res="";
        if(row.isValue(4,"2")){
           res=res+this.findWord(row.find(1))
            res=res+this.findWord(row.find(2))
        }else {
          res=  StrTo.numToUtf8(row.find(1));
        }
        return res;
    }

    /**
     * 获取语法类型
     */
    findYaccType(ids){
        let block=  this.wordr.list[ids];
        let id=0;
        for(let i=0;i<block.list.length;i++){
            let row=block.list[i];
            if(row.isValue(4,"2")){
               id= row.find(1)
            }
        }

      return  this.findWord(id);

    }


    enter(){

    }
    /**
     *  参数都是 buff
     * @param dataRow
     * @param dataBlock
     * @param linkRow
     */
    loadData(data ){
        // this.init();
        this.wordr.loadData(data)
    }

    outData(){
        let buff=this.wordr.outData();
        return  buff;
    }


    /**
     * 初始化
     */
    init(){
        this.wordr=new Wordr();
        // this.addRound(); //目录 可能也不需要、 、


    }

    /**
     * 添加一个词
     */
    addIder(strs){
      let ids= this.run(strs)
      let id=  this.findIderId(strs);
      if(!id){
          id= this.wordr.addData(new Row(ids[0],ids[1],2),new Block());
          this.wordr.blockAddRow(id,new Row(1,1))
       return   id
      }
      return id;
    }
    /**
     * 给一个词添加标记
     */
    iderAddSign(strs,row){
        let ids= this.run(strs)
        let id
        if(ids.length>1){
              id=  this.findIderId(strs);
        }else {
          id=  ids[0]
        }
        this.wordr.blockAddRow(id,row)
    }

    /**
     * 添加 语法类型
     */
    iderAddType(strs,type){
        let ids= this.run(strs)
        let typeIds= this.run(type)
        let id;
        let typeId;
        if(ids.length>1){
            id=  this.findIderId(strs);
        }else {
            id=  ids[0]
        }
        if(typeIds.length>1){
            typeId=  this.findIderId(type)
            if(typeId==null){
              typeId=  this.addIder(type)
            };
        }else {
            typeId=  ids[0]
        }
        this.wordr.blockAddRow(id,new Row(typeId,2))
    }

    /**
     * 获取一个词的ID
     */
    findIderId(strs){
        let ids= this.run(strs)
        return this.lex.findWrodsId(ids[0],ids[1])
    }


    /**
     *
     * @param row
     * @param func
     * @param resType
     * @returns {*}
     */
    findReal(row,realId,func,resType){
        if(realId!=null&&typeof(realId) != "undefined"){
            this.realId=realId;
            return this.wordr.findData(0,null,null,this.realDefaultFunc.bind(this))
        }
        if(resType){
            this.resRealType=resType;
        }else {
            this.resRealType='real';
        }
        if(row){
            this.realRow=row;
        }else {
            this.realRow=new Row(null,null,null,null);
        }
        if(func){
            return this.dar.findData(1,0,null,func)
        }
        return this.wordr.findData(0,null,null,this.realDefaultFunc.bind(this))
    }
    realByIdFunc(roundId,BlockId,RowId,dataThis){
        let   realId=this.realId;
        return dataThis.list[realId];
    }

    realDefaultFunc(roundId,BlockId,RowId,dataThis){
        let word=this.realRow;
        let realId= this.realId;
        if(realId!=null){
            this.realId=null;
            return  dataThis.list[realId];
        }
        for(let n=0;n<dataThis.list.length;n++){
            let square=  dataThis.list[n];
            for(let l=0;l<square.list.length;l++){
                let row=square.list[l];
                let res=[];
                if(row.isValue(1,word.find(1))){
                    res.push(1)
                }
                if(row.isValue(2,word.find(2))){
                    res.push(2)
                }
                if(row.isValue(3,word.find(3)) ){
                    res.push(3)
                }
                if(row.isValue(4,word.find(4))){
                    res.push(4)
                }
                if(res.length==4){
                    if(this.resRealType=='real') {
                        return square;
                    }
                    return l;
                }
            }
        }
        return null;

    }

}